package com.sdy.dcsb.web.controller.inboundservice.util;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 服务请求的一些处理合并
 * </p>
 *
 * @author fb
 * @since 2020/1/9
 */
public class ServiceUtil {

    public static Map<String, String> getServcode(String code) {
        String servcode = null;
        String servversion = null;
        Map<String, String> map = new HashMap<>();
        if(code.contains("@")) {
            if(code.indexOf("@") > 0) {
                servcode = code.substring(0, code.indexOf("@"));
                servversion = code.substring(code.indexOf("@")+1);
                if(isEmpty(servversion)) {
                    servversion = "1.0";
                }
            }
        }else {
            servcode = code;
            servversion = "1.0";
        }
        map.put("servcode", servcode);
        map.put("servversion", servversion);
        return map;
    }

    public static Map<String, String> getQueryString(String queryString, String responseMsg) {
        Map<String, String> map = new HashMap<>();
        if(!isEmpty(queryString)) {
            try {
                queryString = URLDecoder.decode(queryString,"utf-8");
            }catch (IOException e) {
                responseMsg = "URLDecoder.decode失败";
                map.put("code", "1");
            }
        }
        map.put("queryString", queryString);
        map.put("responseMsg", responseMsg);
        return map;
    }

    public static Map<String, String> getContentType(String contentType, HttpServletRequest request) {
        String reqCharset = null;
        Map<String, String> map = new HashMap<>();
        if(null != contentType && contentType.length() > 0) {
            if(contentType.contains(";")) {
                contentType = contentType.substring(0, contentType.indexOf(";"));
            }
            reqCharset = request.getCharacterEncoding();
        }else {
            contentType = "";
            reqCharset = "UTF-8";
        }
        map.put("contentType", contentType);
        map.put("reqCharset", reqCharset);
        return map;
    }

    public static Map<String, String> getRequestMsg(String contentType, HttpServletRequest request, String reqCharset) {
        String requestMsg = "";
        String responseMsg = "";
        Map<String, String> map = new HashMap<>();
        try {
            if(contentType != null && "multipart/form-data".equals(contentType)){
                requestMsg = ServletUtil.getReqFormBodyString(request,"UTF-8");
            }else{
                requestMsg = ServletUtil.readReqBody(request, contentType, reqCharset);
            }
        }catch (Throwable e) {
            responseMsg = e.getMessage();
            map.put("code", "1");
        }
        map.put("requestMsg", requestMsg);
        map.put("responseMsg", responseMsg);
        return map;
    }

}
